package com.neu.ui.home;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ViewFlipper;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.neu.R;
import com.neu.adapter.RecServiceAdapter;
import com.neu.bean.AllService;
import com.neu.bean.Rotation;
import com.neu.bean.Weather;
import com.neu.ui.BaseFragment;
import com.neu.util.VolleyUtil;

import java.util.List;

public class HomeFragment extends BaseFragment {

    private ViewFlipper vf;
    private HomeViewModel homeViewModel;
    private TextView tvWeather;
    private RecyclerView recService;
    @Override
    protected int layoutId() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView(View view) {
        vf=view.findViewById(R.id.vf);
        tvWeather=view.findViewById(R.id.tvWeather);
        recService=view.findViewById(R.id.recService);
        recService.setLayoutManager(new GridLayoutManager(getContext(),6));
    }

    @Override
    protected void initData() {
        homeViewModel=new ViewModelProvider(this,new ViewModelProvider.NewInstanceFactory()).get(HomeViewModel.class);
        //轮播广告
        initRotation();
        //当日天气
        initWeather();
        //获得所有服务
        initAllService();
    }

    private void initAllService() {
        homeViewModel.getAllService().observe(getViewLifecycleOwner(), new Observer<AllService>() {
            @Override
            public void onChanged(AllService allService) {
                List<AllService.RowsBean> list=allService.getRows();
                recService.setAdapter(new RecServiceAdapter(list));
            }
        });
    }

    private void initWeather() {
        homeViewModel.getWeather().observe(getViewLifecycleOwner(), new Observer<Weather>() {
            @Override
            public void onChanged(Weather weather) {
                Weather.DataBean data=weather.getData();
                tvWeather.setText("今日温度："+data.getTemperature()+"     最高温度："+data.getMaxTemperature());
            }
        });
    }

    private void initRotation() {
        homeViewModel.getRotation().observe(getViewLifecycleOwner(), new Observer<Rotation>() {
            @Override
            public void onChanged(Rotation rotation) {
                List<Rotation.RowsBean> list=rotation.getRows();
                for(Rotation.RowsBean r:list){
                    String url=r.getAdvImg();
                    ImageView iv=new ImageView(getContext());
                    iv.setScaleType(ImageView.ScaleType.FIT_XY);
                 //   iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
                    //从网络中下载图片，放入到imageview中
                //    RequestOptions options = new RequestOptions().error(R.drawable.ic_launcher_foreground).bitmapTransform(new RoundedCorners(30));//图片圆角为30

                    RequestOptions requestOptions = new RequestOptions();
                    requestOptions.placeholder(R.drawable.ic_launcher_background);
                    requestOptions.circleCropTransform();
                    requestOptions.transforms( new RoundedCorners(30));



                    Glide.with(getContext())
                            .load(VolleyUtil.BASE_URL+url)
                            .apply(requestOptions)
                            .into(iv);
                    vf.addView(iv);
                }
            }
        });
    }
}